package com.github.chirspan.xaas.soa.message.factory;

import com.github.chirspan.xaas.soa.message.core.MessageModel;
import com.github.chirspan.xaas.soa.message.core.SOAMessage;
import com.github.chirspan.xaas.soa.message.processor.AbstractMessageProcessor;
import com.github.chirspan.xaas.soa.message.processor.MessageProcessor;
import com.github.chirspan.xaas.soa.message.schema.MessageSchemaDefinition;

public class MessageFactory {


    /**
     * 生成消息
     *
     * @param messageType
     * @param message
     * @param msd         msd
     */
    public static SOAMessage generateMessage(String messageType,
                                             MessageModel message, MessageSchemaDefinition msd) {
        if (message != null && msd != null) {
            AbstractMessageProcessor processor = MessageProcessor.getMessageProcessor(messageType);
            if (processor != null) {
                return processor.generate(message, msd);
            }
        }
        return null;
    }

    /**
     *  解析消息
     * @param messageType
     * @param msgStr
     * @param msd
     * @return
     * @throws Exception
     */
    public static MessageModel parseMessage(String messageType, Object msgStr, MessageSchemaDefinition msd) throws Exception {
        if (msgStr != null && msd != null) {
            AbstractMessageProcessor processor = MessageProcessor.getMessageProcessor(messageType);
            if (processor != null) {
                return processor.parse(new SOAMessage(msgStr, msd));
            }
        }
        return null;
    }
}